#include "../inc/app_manager_fs.h"

#if APP_MANAGER_LIB_ENABLE

#if APP_MANAGER_ENABLE_APP_UNINSTALL

static void (*fs_write)(const char *path, const void *data, size_t size) = NULL;
static void (*fs_read)(const char *path, void *data, size_t size) = NULL;

static uint16_t app_datas_lenth = 0;
static char *app_unistall_list = NULL;
static uint8_t uninstall_list_change = 0;

extern void *app_manager_malloc(size_t size);
extern void app_manager_free(void *ptr);

app_manager_ret_t app_manager_fs_write(const char *path, const void *data, size_t size)
{
    if (fs_write != NULL)
        fs_write(path, data, size);
    else
        return APP_MANAGER_FS_ERROR;
    return APP_MANAGER_SUCCESS;
}
app_manager_ret_t app_manager_fs_read(const char *path, void *data, size_t size)
{
    if (fs_read != NULL)
        fs_read(path, data, size);
    else
        return APP_MANAGER_FS_ERROR;
    return APP_MANAGER_SUCCESS;
}

app_manager_ret_t app_manager_register_fs_cb(void (*fs_write_cb)(const char *path, const void *data, size_t size), void (*fs_read_cb)(const char *path, void *data, size_t size))
{
    fs_write = fs_write_cb;
    fs_read = fs_read_cb;
    return APP_MANAGER_SUCCESS;
}

app_manager_ret_t app_manager_fs_init()
{
    if (fs_read == NULL || fs_write == NULL)
        return APP_MANAGER_FS_ERROR;

    fs_read("app_ins_len", &app_datas_lenth, sizeof(uint16_t));

    if (app_datas_lenth == 0)
        return APP_MANAGER_SUCCESS;

    app_unistall_list = malloc_mem(app_datas_lenth);
    if (app_unistall_list == NULL)
        return APP_MANAGER_MALLOC_FAIL;

    fs_read("app_manager_list", app_unistall_list, app_datas_lenth);
}

// 检查应用程序是否已卸载
static app_install_status_t app_chek_uninstalled(const char *app_name)
{
    for (size_t i = 0; i < app_datas_lenth; i++)
    {
        if (app_unistall_list[i] == app_name[0])
        {
            if (strcmp(&app_unistall_list[i], app_name) == 0)
            {
                return APP_UNINSTALLED;
            }
            i += strlen(app_name) + 1;
        }
    }
    return APP_INSTALLED;
}

uint8_t app_manager_get_save_uninstalled_number()
{
    if (app_unistall_list == NULL)
        return 0;
    uint8_t cnt = 0;
    for (size_t i = 0; i < app_datas_lenth; i++)
    {
        if (app_unistall_list[i] == '\0')
            cnt++;
    }
    return cnt;
}

app_manager_ret_t app_manager_get_save_uninstalled_name(uint8_t app_id, char *app_name)
{
    if (app_unistall_list == NULL)
        return APP_MANAGER_NAME_NONE;

    uint8_t cnt = 0;

    for (size_t i = 0; i < app_datas_lenth;)
    {
        if (app_id == cnt)
        {
            strcpy(app_name, &app_unistall_list[i]);
            return APP_MANAGER_SUCCESS;
        }
        i += strlen(&app_unistall_list[i]) + 1;
        cnt++;
    }
    return APP_MANAGER_NAME_NONE;
}

app_manager_ret_t app_manager_fs_save_change_data()
{
    if (fs_read == NULL || fs_write == NULL)
        return APP_MANAGER_FS_ERROR;

    if (uninstall_list_change == 0)
        return APP_MANAGER_SUCCESS;

    fs_write("app_manager_list", app_unistall_list, app_datas_lenth);
    fs_write("app_manager_lenth", &app_datas_lenth, sizeof(uint16_t));

    return APP_MANAGER_SUCCESS;
}

#endif // #if APP_MANAGER_ENABLE_APP_UNINSTALL
#endif // #if APP_MANAGER_LIB_ENABLE